package com.example.douyin.db;

import android.app.Application;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

public class DBUtils {
    private DBUtils(){}
    private static DBUtils dbUtils = new DBUtils();
    public static DBUtils getInstance(){
        return dbUtils;
    }
    //实例化数据库对象以及工具类
    private APPDataBase appDataBase;
    private UserDao userDao;
    private VideoDao videoDao;
    private MoneyDao moneyDao;

    public void init(Application application){
        appDataBase = Room.databaseBuilder(application,APPDataBase.class,"user.db")
                .allowMainThreadQueries()//可以在主线程执行数据库操作
                .build();
        userDao = appDataBase.getUserDao();
        videoDao  =appDataBase.getVideoDao();
        moneyDao = appDataBase.getMoneyDao();
    }
    //防止内存不足时，导致Dao被回收，重新初始化一下
    public UserDao getUserDao(){
        if(userDao==null){
            userDao = appDataBase.getUserDao();
        }
        return userDao;
    }
    public VideoDao getVideoDao(){
        if(videoDao==null){
            videoDao = appDataBase.getVideoDao();
        }
        return videoDao;
    }

    public MoneyDao getMoneyDao(){
        if(moneyDao==null){
            moneyDao = appDataBase.getMoneyDao();
        }
        return moneyDao;
    }

}
